package infostroy.bookagolf.components.inputs;

import infostroy.bookagolf.UI.GolfRoundedButtonUI;
import infostroy.bookagolf.common.Constants.Colors;
import infostroy.bookagolf.common.Constants.Icons;
import infostroy.bookagolf.common.helpers.ImageHelper;
import infostroy.bookagolf.components.borders.RoundedBorder;
import infostroy.bookagolf.components.panels.GolfRightRoundPanel;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.basic.BasicSpinnerUI;
import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * Created by valer_000 on 2/1/14.
 */
public class GolfSpinner extends JSpinner implements Colors {

    public GolfSpinner() {
        setEditor(new Editor(this));
        setBorder(new RoundedBorder());
        setUI(new GolfSpinnerUI());
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(super.getPreferredSize().width, 30);
    }

    @Override
    public Dimension getMaximumSize() {
        return new Dimension(super.getMaximumSize().width, 30);
    }

    private class GolfSpinnerUI extends BasicSpinnerUI {

        @Override
        protected Component createPreviousButton() {
            Component component = createButton(Icons.ARROW_DOWN);
            if (component != null) {
                GolfRoundedButtonUI ui = new GolfRoundedButtonUI(false, false, false, true, false);
                ui.setMargin(0, 2, 2, 0);
                ((JButton) component).setUI(ui);
                installPreviousButtonListeners(component);
            }
            return component;
        }

        @Override
        protected Component createNextButton() {
            Component component = createButton(Icons.ARROW_UP);
            if (component != null) {
                GolfRoundedButtonUI ui = new GolfRoundedButtonUI(false, true, false, false, true);
                ui.setMargin(2, 2, 0, 0);
                ((JButton) component).setUI(ui);
                installNextButtonListeners(component);
            }
            return component;
        }

        private Component createButton(String url) {
            BufferedImage icon = ImageHelper.getInstance().loadImage(url);
            if (icon != null) {
                JButton arrowButton = new JButton(new ImageIcon(icon));
                arrowButton.setPreferredSize(new Dimension(icon.getWidth() + 20, icon.getHeight()));
                arrowButton.setBorderPainted(false);
                arrowButton.setFocusPainted(false);
                arrowButton.setContentAreaFilled(false);
                return arrowButton;
            } else {
                return null;
            }
        }

    }

    private class Editor extends GolfRightRoundPanel implements ChangeListener {

        private JLabel label = new JLabel();

        private Editor(JSpinner spinner) {
            setLayout(new FlowLayout(FlowLayout.LEADING));
            label.setForeground(Colors.TABS_BACKGROUND);
            label.setFont(UIManager.getDefaults().getFont("TextField.font"));
            add(label);
            spinner.addChangeListener(this);
        }

        @Override
        public void stateChanged(ChangeEvent e) {
            JSpinner spinner = (JSpinner) e.getSource();
            label.setText(spinner.getValue().toString());
        }
    }
}
